/**
 * Created by steven on 2016/4/22.
 */


$("#search_btn").on('click',function() {
    $("#judge_doc_form").submit();
});

var total=$('#totalcount').html();
var pages=get_pages(total,15);
var curpage=2;
var flag=true;
window.onscroll = function () {

    //监听事件内容

    if(getScrollHeight() >= getWindowHeight() + getDocumentTop()-10){
        //当滚动条到底时,这里是触发内容
        //异步请求数据,局部刷新dom
        //ajax_function()
        if(!flag){
            return;
        }
        flag=false;
        if(!(curpage<pages+1)){
            flag=true;
            return;
        }
        $('#loadingInfo').show();
        loadMoreData();
    }
}

function get_pages(total,pagesize){
    var page=total%pagesize==0 ? total/pagesize : Math.ceil(total/pagesize);
    return page;
}

//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}

//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;    return scrollHeight;
}

function loadMoreData(){
    var cbfy=$('#cbfy').val();
    var ah=$('#ah').val();
    var url='index.php/JudgeDocument/judge_doc_list';
    var dt={
        pageno:curpage,
        pagesize:15,
        cbfy:cbfy,
        ah:ah,
        isAjax:true
    };
    var aj = $.ajax( {
        url:url,// 跳转到 action
        data:dt,
        type:'post',
        cache:false,
        dataType:'json',
        success:function(data) {
            var isSuccess=data.isSuccess;
            if(!isSuccess){
                giveAlert(data.errorMessage);
            }else{
                renderhtml(data.data.list);
                $('#loadingInfo').hide();
                curpage+=1;
                flag=true;
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(XMLHttpRequest.status);
            //alert(XMLHttpRequest.readyState);
            //alert(textStatus);
            $('#loadingInfo').hide();
            curpage+=1;
            flag=true;
        }
    });
}

function renderhtml(obj){
    var str='';
    for(var i=0;i<obj.length;i++){
        str+='<a href="index.php/JudgeDocument/detail/'+obj[i].DocumentId +' " class="list-group-item" >';
        str+='<div class="row" style="margin-bottom: 10px;">';
        str+='<div >';
        str+='<div class="col-xs-3">';
        str+='<span>案件号</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<span class="ah">'+obj[i].AH +'</span>';
        str+='</div>';
        str+='</div>';
        str+='<div >';
        str+='<div class="col-xs-3">';
        str+='<span>结案日期</span>';
        str+='</div>';
        str+='<div class="col-xs-9">';
        str+='<div class="col-xs-11" style="padding-left: 0px">';
        str+=obj[i].JARQ;
        str+='</div>';
        str+='<div style="text-align:center;">';
        str+='<span class="glyphicon glyphicon-menu-right" aria-hidden="true"></span>';
        str+='</div>';
        str+='</div>';
        str+='</div>';
        str+='</div>';
        str+='</a>';
    }
    $('ul.list-group').append(str);
}